package writer;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import bean.QuestionBean;

public class XmlWriter {
	private DocumentBuilderFactory docFactory;
	private DocumentBuilder docBuilder;
	private Document doc;
	private static XmlWriter writer;
	private Element rootElement;
	private Integer count = 1;
	private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	public static XmlWriter getIstance() {
		if (writer == null) {
			writer = new XmlWriter();
		}
		return writer;
	}

	private XmlWriter() {
		Date date = new Date();
		try {
			docFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docFactory.newDocumentBuilder();

			doc = docBuilder.newDocument();

			rootElement = doc.createElement("Game");
			doc.appendChild(rootElement);
			
			Attr attr = doc.createAttribute("startTime");
			attr.setValue(dateFormat.format(date));
			rootElement.setAttributeNode(attr);
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		}
	}

	public void addNode(QuestionBean guestionBeanCurent, String ansver,
			Integer curentScore) {
		boolean isTrueAnswer = false;
		
		Date date = new Date();

		Attr questionAttribut = doc.createAttribute("Nr");
		questionAttribut.setValue(count.toString());
		
		Element nrQuestion = doc.createElement("NrQuestion");
		nrQuestion.setAttributeNode(questionAttribut);
		count++;
		rootElement.appendChild(nrQuestion);
		
		Element question = doc.createElement("Question");		
		question.appendChild(doc.createTextNode(guestionBeanCurent.getQuestion()));
		nrQuestion.appendChild(question);

		Element userAnswer = doc.createElement("userAnswer");
		userAnswer.appendChild(doc.createTextNode(ansver));
		nrQuestion.appendChild(userAnswer);

		Element truectAnswer = doc.createElement("isTrueUserAnswer");
		if (ansver.equals(guestionBeanCurent.getTrueResponse())) {
			isTrueAnswer = true;
		}
		String s = new Boolean(isTrueAnswer).toString();
		truectAnswer.appendChild(doc.createTextNode(s));
		nrQuestion.appendChild(truectAnswer);

		Element timeAnswer = doc.createElement("timeAnswer");
		timeAnswer.appendChild(doc.createTextNode(dateFormat.format(date)));
		nrQuestion.appendChild(timeAnswer);

		Element pointAtMoment = doc.createElement("curentNumberOfPoints");
		pointAtMoment.appendChild(doc.createTextNode(curentScore.toString()));
		nrQuestion.appendChild(pointAtMoment);
	}

	public void saveXml() {
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);

			StreamResult result = new StreamResult(new File("C:\\testing.xml"));
			transformer.transform(source, result);
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}

	}
}
